// pages/login/login.js
const app = getApp();
Page({
  data: {
    userName: false,
    userPassword: false,
    username: '',
    password: ''
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
   // 获取输入账号 
   usernameInput: function (e) {
    this.setData({
      username: e.detail.value
    })
  },
 
  // 获取输入密码 
  passwordInput: function (e) {
    this.setData({
      password: e.detail.value
    })
  },

  login: function (params) {
    wx.navigateTo({
      url: '../index/index',
    })
  },
  formSubmit: function (e) {
    let that = this;
    let userName = that.data.username;
    let userPassword = that.data.password;
    if (userName == '') {
      this.setData({
        userName: true,
      })
      return false;
    } else {
      this.setData({
        userName: false,
      })
    }
    if (userPassword == '') {
      this.setData({
        userPassword: true,
      })
      return false;
    } else {
      this.setData({
        userPassword: false,
      })
    }

    wx.request({
      url: 'http://47.111.122.217:9001/info/transportWorker/login',
      method: 'post',
      data: {
        'password': userPassword,
        'username': userName
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        if (res.data.success) {
          // console.log(res.data.data.data.id);
          // console.log(res.data.data.data.name);
          app.globalData.user = {
            'id': res.data.data.data.id,
            'name': res.data.data.data.name
          };
          that.login();
        } else {
          wx.showToast({
            title: '账号密码错误',
            icon: 'error',
            duration: 2000
          })
        }
      }
    })
  }
})